package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.ComEnvironmentallyExamineMapper;
import com.bf.electroplating.pojo.dto.environmentally.EnvironmentallyExamineDTO;
import com.bf.electroplating.pojo.dto.environmentally.EnvironmentallyExamineQueryDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ComEnvironmentallyExamine;
import com.bf.electroplating.pojo.enums.AttModuleEnums;
import com.bf.electroplating.pojo.enums.ProjectTypeEnum;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.environmentally.EnvironmentallyExamineVO;
import com.bf.electroplating.service.ComCompanyService;
import com.bf.electroplating.service.ComEnvironmentallyExamineService;
import com.bf.electroplating.service.PubAttachmentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.RegEx;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author 15451
 * @description 针对表【com_environmentally_examine(环境审查记录表)】的数据库操作Service实现
 * @createDate 2025-05-09 11:11:32
 */
@Service
@Slf4j
public class ComEnvironmentallyExamineServiceImpl extends ServiceImpl < ComEnvironmentallyExamineMapper,
        ComEnvironmentallyExamine > implements ComEnvironmentallyExamineService {

	@Resource
	private ComCompanyService comCompanyService;
	@Resource
	private PubAttachmentService pubAttachmentService;

	@Override
	public ResultVO < Page < EnvironmentallyExamineVO > > pageList ( EnvironmentallyExamineQueryDTO queryDTO ) {
		Page < ComEnvironmentallyExamine > page = new Page <> ( queryDTO.getPageNum () , queryDTO.getPageSize () );
		Map < String,String > companyMap = comCompanyService.list ().stream ().collect ( Collectors.toMap ( ComCompany::getId , ComCompany::getCompanyName ) );
		LambdaQueryWrapper < ComEnvironmentallyExamine > wrapper = new LambdaQueryWrapper <> ();
        if ( StringUtils.isNotBlank ( queryDTO.getCompanyId () ) ) {
            wrapper.eq ( ComEnvironmentallyExamine::getCompanyId , queryDTO.getCompanyId () );
        }
		if ( StringUtils.isNotBlank ( queryDTO.getEiaName () ) ) {
			wrapper.like ( ComEnvironmentallyExamine::getEiaName , queryDTO.getEiaName () );
		}
		if ( StringUtils.isNotBlank ( queryDTO.getProjectType () ) ) {
			wrapper.eq ( ComEnvironmentallyExamine::getProjectType , queryDTO.getProjectType () );
		}
		wrapper.orderByDesc ( ComEnvironmentallyExamine::getCreateTime );

		Page < ComEnvironmentallyExamine > resultPage = this.page ( page , wrapper );

		// 转换为VO
		Page < EnvironmentallyExamineVO > voPage = new Page <> ();
		BeanUtils.copyProperties ( resultPage , voPage , "records" );

		List < EnvironmentallyExamineVO > voList = new ArrayList <> ();
		for ( ComEnvironmentallyExamine entity : resultPage.getRecords () ) {
			EnvironmentallyExamineVO vo = new EnvironmentallyExamineVO ();
			BeanUtils.copyProperties ( entity , vo );
			vo.setProjectType(ProjectTypeEnum.getDesc ( entity.getProjectType () ));
			vo.setCompanyName ( companyMap.get ( entity.getCompanyId () ) );
			vo.setAcceptanceFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.ACCEPTANCE.getCode () ) );
			vo.setEiaFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.EIA.getCode () ) );
			vo.setApprovalFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.APPROVAL.getCode () ) );
			voList.add ( vo );
		}
		voPage.setRecords ( voList );

		return ResultVO.ok ( voPage );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > saveData ( EnvironmentallyExamineDTO dto ) {
		ComEnvironmentallyExamine examine = new ComEnvironmentallyExamine ();
		BeanUtils.copyProperties ( dto , examine );
		saveOrUpdate ( examine );

		pubAttachmentService.saveWholeAttachmentRel ( dto.getAcceptanceFile () , examine.getId () , AttModuleEnums.ACCEPTANCE.getCode () );
		pubAttachmentService.saveWholeAttachmentRel ( dto.getEiaFile () , examine.getId () , AttModuleEnums.EIA.getCode () );
		pubAttachmentService.saveWholeAttachmentRel ( dto.getApprovalFile () , examine.getId () , AttModuleEnums.APPROVAL.getCode () );

		return ResultVO.ok ( null , "保存成功" );
	}


	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > delete ( List < String > ids ) {
		if ( ids == null || ids.isEmpty () ) {
			return ResultVO.fail ( "ID不能为空" );
		}

		this.removeByIds ( ids );
		return ResultVO.ok ( null , "删除成功" );
	}

	@Override
	public ResultVO < EnvironmentallyExamineVO > getDetail ( String id ) {
		if ( StringUtils.isBlank ( id ) ) {
			return ResultVO.fail ( "ID不能为空" );
		}
		ComEnvironmentallyExamine entity = this.getById ( id );
		if ( entity == null ) {
			return ResultVO.fail ( "记录不存在" );
		}
		Map < String,String > companyMap = comCompanyService.list ().stream ().collect ( Collectors.toMap ( ComCompany::getId , ComCompany::getCompanyName ) );
		EnvironmentallyExamineVO vo = new EnvironmentallyExamineVO ();
		BeanUtils.copyProperties ( entity , vo );
		vo.setCompanyName ( companyMap.get ( entity.getCompanyId () ) );
		vo.setAcceptanceFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.ACCEPTANCE.getCode () ) );
		vo.setEiaFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.EIA.getCode () ) );
		vo.setApprovalFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.APPROVAL.getCode () ) );
		return ResultVO.ok ( vo );
	}


}




